package com.controller.rd;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.model.rd.PlanMm;
import com.model.rd.PlanVoModel;
import com.service.rd.PlanService;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
 * 采购计划管理
 */
@Controller("rdPlanController")
@RequestMapping(value = "/api")
public class PlanController {

	static final Logger logger = LoggerFactory.getLogger(PlanController.class);

	@Autowired
	PlanService planService;

	/**
	 * 查询明细列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/plan/selectPlanMmList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectPlanMmList(@RequestParam Map<String, Object> map) {
		return planService.selectPlanMmList(map);
	}

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/plan/selectPlanList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectPlanList(@RequestParam Map<String, Object> map) {
		return planService.selectPlanList(map);
	}

	/**
	 * 保存明细
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/plan/savePlan", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult savePlan(@RequestBody PlanVoModel purchasePlanVo) {
		return planService.savePlan(purchasePlanVo);
	}

	/**
	 * 回退
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/plan/backPlan", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult backPlan(@RequestParam String planId) {
		return planService.backPlan(planId);
	}

	@RequestMapping(value = "rd/plan/delete", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delete(@RequestParam String planId) {
		return planService.delete(planId);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/rd/plan/exportPlan")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购计划-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		planService.exportPlan(map, response);

	}
}
